/**
 * 
 */
package com.base.myproject.client.busi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 此类是在有新用户使用的情况下，由软件维护人员添加新的用户系统管理员和公司代码
 * 
 * @author zgw
 *2009-11-3 下午04:52:29
 * javazgw@gmail.com
 */
public class BcodeRootPanel extends BaseJDBCPanel implements Listener<BaseEvent> {

	Button addbutton ;
	Button delbutton ;
	Button savebutton;
	TextField<String> ccode_t ;
	TextField<String> bname_t ;
	TextField<String> sex_t;
	TextField<String> edu_t ;
	DateField bird_t ;
	DateField incom_t ;
	TextField<String> mobil_t ;
	TextField<String> idno_t ;
	TextField<String> liveaddr_t ;
	TextField<String> systemid_t ;
	TextField<String> password_t ;
	
	
	BaseBusiLeftPanel basbusileftpanel;
	
	BaseStore<BusiObject> store = new BaseStore<BusiObject>();
	BaseStore<BusiObject> systemstore;
	public BcodeRootPanel()
	{
		super();
		init();
	}
	public void init()
	{
		this.setWidth(Window.getClientWidth());
		this.setAutoHeight(true);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	
	    this.add(initWeiget(),centerData);
		
		registerMainStore(getStore());
		registerStore(getStore());
		registerStore(systemstore);
		this.setBusino(busi);
//		this.setWidth("1024px");
//		this.setHeight("720px");
		//this.setHeight("600");
		
		setdefault();
	}
	private void setdefault()
	{
		//Env.get
		getStore().addDefault("systemid", "21");
	}
	public ContentPanel initWeiget() 
	{
//		 VerticalPanel vpanle = new VerticalPanel();
//		 HorizontalPanel hpanel = new HorizontalPanel();
//		 hpanel.add(CreateCcodeLeftPanel());
//		 hpanel.add(CreateBcodeForm());
/*		ContentPanel cpanel = new ContentPanel();
		cpanel.add(hpanel);*/
		
		
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
//		panel.setLayout(new BorderLayout());
//		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 250);
//		westData.setSplit(true);
//		westData.setCollapsible(true);
//		westData.setMargins(new Margins(0));
//		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
//		centerData.setMargins(new Margins(5, 0, 5, 0));
//		panel.add(CreateCcodeLeftPanel(), westData);
//		panel.add(CreateBcodeForm(), centerData);
//		
		

		TableLayout tl = new TableLayout(2);
		
		panel.setLayout(tl);
		TableData tdleft = new TableData();
		tdleft.setRowspan(2);
		tdleft.setColspan(1);
		tdleft.setWidth("200px");
		//tdleft.setHeight("700px");
		
		panel.add(CreateCcodeLeftPanel(),tdleft);
		TableData tdright = new TableData();
		tdright.setRowspan(1);
		tdright.setColspan(1);
	//	tdright.setWidth("500px");
	//	tdright.setHeight("700px");
		tl.setCellHorizontalAlign(HorizontalAlignment.LEFT);
		tl.setCellVerticalAlign(VerticalAlignment.TOP);
		//tl.setHeight("700px");
	
		tl.setCellSpacing(5);
		
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.add(CreateBcodeForm());
		vpanel.add(CreateSysteminfo());
		panel.add(vpanel,tdright);
		panel.setHeight("720");
		return panel;
		
	}
	 
	 public ContentPanel CreateCcodeLeftPanel() {
			List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
			ColumnConfig cname = new ColumnConfig("bcode", "编号", 50);
		

			columns.add(cname);
			columns.add(new ColumnConfig("bname", "名字", 50));

			ColumnModel cm = new ColumnModel(columns);

			// --set modetype
			ModelType type = new ModelType();
			type.setRoot("records");
			type.setRecordName("record");
			type.setTotalName("totle");
			type.addField("icode");
			type.addField("bcode");
			type.addField("bname");
			type.addField("departments");
			type.addField("password");
			type.addField("email");
			type.addField("sex");
			type.addField("edu");
			type.addField("bird");
			type.addField("incom");
			type.addField("mobil");
			type.addField("idno");
			type.addField("status");
		
			type.addField("liveaddr");
			type.addField("systemid");
			
			
			type.getField("bird").setType(Date.class);
			type.getField("incom").setType(Date.class);
			type.getField("systemid").setType(Integer.class);
			// type.addField("Phone");
			// type.addField("imagepath");
			StoreInfo info = new StoreInfo();
			info.setTablename("bcode");
			info.setSql("select * from bcode ");
			info.setKeyname(new String[] { "icode" });
			 basbusileftpanel = new BaseBusiLeftPanel(columns,
					type, info);
			setStore(basbusileftpanel.getStore());
			//getStore().setKeyname(new String[] { "codename" });

			basbusileftpanel.getGrid().setAutoExpandColumn("codename");
			setMaincomponent(basbusileftpanel.getGrid());
			return basbusileftpanel.getPanel();
		}
	 BaseFormPanel CreateBcodeForm()
	{
		
		
		
		BaseFormPanel bfp = new BaseFormPanel(getStore());
		
		Label ccode_l = new Label("编号");
		ccode_l.setWidth("50");
		Label sex_l = new Label("性别");
		sex_l.setWidth("50");
		Label bname_l = new Label("名字");
		bname_l.setWidth("50");

		Label edu_l = new Label("学历");
		Label bird_l = new Label("出生日期");
		Label incom_l = new Label("入司日期");
		Label mobil_l = new Label("手机号");
		Label idno_l = new Label("身份证号");
		Label liveaddr_l = new Label("居住地址");
		Label systemid_l = new Label("系统id");
		Label password_l = new Label("管理员密码");
		 ccode_t = new TextField<String>();
		 bname_t = new TextField<String>();
		 sex_t = new TextField<String>();
		 edu_t = new TextField<String>();
		 bird_t = new DateField();
		 bird_t.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d"));  
		 incom_t = new DateField();
		 incom_t.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d"));  
		 mobil_t = new TextField<String>();
		 idno_t = new TextField<String>();
		 liveaddr_t = new TextField<String>();
		 systemid_t = new TextField<String>();
		 password_t= new TextField<String>();
		 
		 systemid_t.setAllowBlank(false);
		 password_t.setAllowBlank(false);
		 
		 ccode_t.setName("bcode") ;
		 bname_t.setName("bname") ;
		 sex_t.setName("sex");
		 edu_t.setName("edu") ;
		 bird_t.setName("bird") ;
		 incom_t.setName("incom") ;
		 mobil_t.setName("mobil") ;
		 idno_t.setName("idno") ;
		 liveaddr_t.setName("liveaddr") ;
		 systemid_t.setName("systemid") ;
		 password_t.setName("password") ;
		 
		//this.setHeaderVisible(false);
		this.setHeight("200");
		TableLayout tl = new TableLayout(6);
		tl.setWidth("600");
		bfp.setLayout(tl);
		TableData td=new TableData();
		td.setColspan(1);
	//	td.setWidth("600");
		td.setMargin(100);
		td.setPadding(5);
		td.setRowspan(1);
		
		bfp.add(ccode_l,td);
		bfp.add(ccode_t,td);
		bfp.add(bname_l,td);
		bfp.add(bname_t,td);
		bfp.add(sex_l,td);
		bfp.add(sex_t,td);
		bfp.add(edu_l,td);
		bfp.add(edu_t,td);
		bfp.add(bird_l,td);
		bfp.add(bird_t,td);
		bfp.add(incom_l,td);
		bfp.add(incom_t,td);
		bfp.add(mobil_l,td);
		bfp.add(mobil_t,td);
		bfp.add(idno_l,td);
		bfp.add(idno_t,td);
		bfp.add(liveaddr_l,td);
		bfp.add(liveaddr_t,td);
		bfp.add(systemid_l,td);
		bfp.add(systemid_t,td);
		bfp.add(password_l,td);
		bfp.add(password_t,td);
		
		HorizontalPanel hpanel = new HorizontalPanel();
		 addbutton = new Button("添加");
		 delbutton = new Button("删除");
		 savebutton = new Button("保存");
		
		hpanel.add(addbutton);
		hpanel.add(delbutton);
		hpanel.add(savebutton);
		addbutton.addListener(Events.Select, this);
		delbutton.addListener(Events.Select, this);
		savebutton.addListener(Events.Select, this);
		bfp.add(hpanel,td);
		bfp.inited();
		return bfp;
	}
	
	 Widget CreateSysteminfo()
	 {
			ModelType type = new ModelType();
			type.setRoot("records");
			type.setRecordName("record");

			type.addField("icode");
			type.addField("id");
			type.addField("name");

			
			
			type.getField("id").setType(String.class);
			type.getField("name").setType(String.class);
			// type.addField("Phone");
			// type.addField("imagepath");
			StoreInfo info = new StoreInfo();
			info.setTablename("systeminfo");
			info.setSql("select * from systeminfo where id='{systemid}'");
			info.setKeyname(new String[] { "icode" });
			info.setNeedAllRow("no");
			info.setParent(getStore());
			info.setLinkparentcolumns(new String[]{"systemid"});
			info.setLinkparent_chile_columns(new String[]{"id"});
			systemstore = new BaseStore<BusiObject>(type,info);
			Debug.printf("systemstore is null ="+systemstore, 4);
			BaseFormPanel bfp = new BaseFormPanel(systemstore);
			Label id_l = new Label("id");
			id_l.setWidth("50");
			Label name_l = new Label("公司名");
			name_l.setWidth("50");
			TextField<String> id_t = new TextField<String>();
			TextField<String> name_t = new TextField<String>();
			id_t.setName("id") ;
			name_t.setName("name") ;
			
			TableLayout tl = new TableLayout(4);
			tl.setWidth("600");
			bfp.setLayout(tl);
			TableData td=new TableData();
			td.setColspan(1);
		//	td.setWidth("600");
			td.setMargin(100);
			td.setPadding(5);
			td.setRowspan(1);
			bfp.add(id_l,td);
			bfp.add(id_t,td);
			bfp.add(name_l,td);
			bfp.add(name_t,td);
			
			
			bfp.inited();
			return bfp;
	 }
	/**
	 * @return the store
	 */
	public BaseStore<BusiObject> getStore() {
		return store;
	}
	/**
	 * @param store the store to set
	 */
	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}
//	void reset()
//	{
//		 ccode_t.setValue("") ;
//		 bname_t.setValue("") ;
//		 sex_t.setValue("");
//		 edu_t.setValue("") ;
////		 bird_t.setValue("") ;
////		 incom_t.setValue("") ;
//		 mobil_t.setValue("") ;
//		 idno_t.setValue("") ;
//		 liveaddr_t.setValue("") ; 
//	}

	@Override
	public void handleEvent(BaseEvent be) {
		
		if(be.getSource().equals(addbutton))
		{
			basbusileftpanel.getGrid().addnewrecord();
		}
		else if(be.getSource().equals(delbutton))
		{
			basbusileftpanel.getGrid().deleterecord();
		}
		else if(be.getSource().equals(savebutton))
		{
			getStore().trySave();
		}
	}
	
}
